/**
 * 通过 mutations 间接更新 state的多个方法的对象
 */
import {
  RECEIVE_USER_INFO,
  RESET_USER_INFO,
  RECEIVE_MENU_LIST,
  ERROR_MSG,
  RECEIVE_USER_PAGE,
  UPDATE_USER_STATE,
  SUCCESS_MSG
} from '../store/mutation-types'

import {
  reqMenuList,
  reqUserPage,
  reqUpdateUserState
} from '../api'

export default {
  // 同步记录用户信息
  recordUser ({ commit }, userInfo) {
    commit(RECEIVE_USER_INFO, { userInfo })
  },
  // 同步登出
  logout ({ commit }, userInfo) {
    commit(RESET_USER_INFO, { userInfo })
  },
  // 异步获取左侧菜单列表
  async getMenuList({ commit }) {
    const { data, meta } = await reqMenuList()
    // 提交一个 mutation
    if (meta.status === 200) { // success
      commit(RECEIVE_MENU_LIST, { menuList: data })
    } else { // fail
      commit(ERROR_MSG, { msg: meta.msg })
    }
  },
  // 异步获取分页对象
  async getUserPage({ commit }, { query, pagenum, pagesize }) {
    const { data, meta } = await reqUserPage({ query, pagenum, pagesize })
    if (meta.status === 200) {
      commit(RECEIVE_USER_PAGE, data)
    } else {
      commit(ERROR_MSG, meta)
    }
  },
  // 异步更新用户的状态
  async updateUserState({ commit }, { uid, state }) {
    const { data, meta } = await reqUpdateUserState({ uid, state })
    if (meta.status === 200) {
      commit(UPDATE_USER_STATE, data)
      commit(SUCCESS_MSG, meta)
    } else {
      commit(ERROR_MSG, meta)
    }
  }/* ,
  // 异步获取用户信息
  async getUserInfo({ commit }, { id }) {
    const { data, meta } = await reqUserInfo(id)
    if (meta.status === 200) {
      commit(RECEIVE_USER_INFO, { userInfo: data })
    } else {
      commit(ERROR_MSG, meta)
    }
  } */
}
